<template>
	<view class="step1">
		<view class="item-wrapper">
			<view class="title-wrapper">
				<view class="title">
					供应商名称
				</view>
				<view class="value small">
					{{ form.supplierName.length }}/15
				</view>
			</view>
			<view class="input-wrapper">
				<input v-model="form.supplierName" type="text" maxlength="15" class="input"
					placeholder="店铺招牌,公司简称等,与他人不冲突,与店铺有相关性" />
			</view>
		</view>
		<view class="item-wrapper">
			<view class="title-wrapper">
				<view class="title">
					经营地址
				</view>
				<view class="value small">
					{{ form.businessAddress.length }}/50
				</view>
			</view>
			<view class="input-wrapper">
				<textarea v-model="form.businessAddress" maxlength="50" class="text-area"
					placeholder="店铺详细经营地址"></textarea>
			</view>
		</view>
		<view class="item-wrapper">
			<view class="title-wrapper">
				<view class="title">
					管理员
				</view>
			</view>
			<view class="form-wrapper">
				<view class="form-item">
					<view class="form-label">
						姓名
					</view>
					<view class="form-value">
						<input v-model="form.manager" type="text" maxlength="5" class="input" placeholder="管理员真实姓名" />
					</view>
				</view>
				<view class="form-item">
					<view class="form-label">
						手机
					</view>
					<view class="form-value">
						<input v-model="form.managerTel" type="number" maxlength="11" class="input"
							placeholder="管理员手机号码" />
					</view>
				</view>
			</view>
		</view>
		<view class="item-wrapper">
			<view class="title-wrapper">
				<view class="title">
					客服电话
				</view>
				<view class="value small">
					{{ form.serviceTel.length }}/11
				</view>
			</view>
			<view class="input-wrapper">
				<input v-model="form.serviceTel" type="number" maxlength="11" class="input" placeholder="客户服务电话" />
			</view>
		</view>
		<view class="item-wrapper">
			<view class="title-wrapper" @click="goCategory">
				<view class="title">
					经营类目
				</view>
				<view class="value">
					请选择
				</view>
				<uni-icons type="right" color="#898989" size="18"></uni-icons>
			</view>
			<view class="input-wrapper">
				<textarea disabled v-model="form.businessCategoryName" maxlength="50" class="text-area"
					placeholder="供货的产品类目"></textarea>
			</view>
		</view>
		<view class="item-wrapper">
			<view class="title-wrapper" @click="goBusinessQualification">
				<view class="title">
					经营资质
				</view>
				<view class="value">
					{{ form.businessQualification ? '去修改':'请上传' }}
				</view>
				<uni-icons type="right" color="#898989" size="18"></uni-icons>
			</view>
			<view class="input-wrapper">
				<input disabled type="text" class="input" placeholder="行政许可证书、品牌授权证书等" />
			</view>
		</view>
		<view class="item-wrapper">
			<view class="title-wrapper" @click="selectSubjectType">
				<view class="title">
					主体类型
				</view>
				<view v-if="form.subjectType === 1" class="value black">
					自然人
				</view>
				<view v-else-if="form.subjectType === 3" class="value black">
					个体户
				</view>
				<view v-else-if="form.subjectType === 2" class="value black">
					企业
				</view>
				<view v-else class="value">
					请选择
				</view>
				<uni-icons type="right" color="#898989" size="18"></uni-icons>
			</view>
			<!-- <view class="tip">
				即店铺名称或营销号名称，将在微信支付页面展示给消费者，开户成功后不支持修改。
			</view> -->
		</view>
		<view class="item-wrapper" v-if="form.subjectType > 1">
			<view class="title-wrapper" @click="goOrg">
				<view class="title">
					经营主体
				</view>
				<view class="value">
					{{ supplierSubject.subjectName ? '去修改' : '请完善' }}
				</view>
				<uni-icons type="right" color="#898989" size="18"></uni-icons>
			</view>
			<view class="form-wrapper">
				<view class="form-item">
					<view class="form-label">
						主体全称
					</view>
					<view class="form-value">
						{{ supplierSubject.subjectName || '-' }}
					</view>
				</view>
			</view>
		</view>
		<view class="item-wrapper" v-if="form.subjectType > 1" @click="goLegal">
			<view class="title-wrapper">
				<view class="title">
					法人代表
				</view>
				<view class="value">
					{{ supplierLegal.realName ? '去修改' : '请完善' }}
				</view>
				<uni-icons type="right" color="#898989" size="18"></uni-icons>
			</view>
			<view class="form-wrapper">
				<view class="form-item">
					<view class="form-label">
						姓名
					</view>
					<view class="form-value">
						{{ supplierLegal.realName || '-' }}
					</view>
				</view>
			</view>
		</view>
		<view class="item-wrapper" v-if="form.subjectType === 1">
			<view class="title-wrapper" @click="goAdmin">
				<view class="title">
					负责人
				</view>
				<view class="value">
					{{ supplierAdmin.realName ? '去修改' : '请完善' }}
				</view>
				<uni-icons type="right" color="#898989" size="18"></uni-icons>
			</view>
			<view class="form-wrapper">
				<view class="form-item">
					<view class="form-label">
						姓名
					</view>
					<view class="form-value">
						{{ supplierAdmin.realName || '-' }}
					</view>
				</view>
				<!-- <view class="form-item">
					<view class="form-label">
						手机
					</view>
					<view class="form-value">
						{{ supplierAdmin.phone || '-' }}
					</view>
				</view> -->
			</view>
		</view>
		<view class="item-wrapper">
			<view class="title-wrapper" @click="goBank">
				<view class="title">
					收款账户
				</view>
				<view class="value">
					{{ supplierBank.accountName ? '去修改' : '请完善' }}
				</view>
				<uni-icons type="right" color="#898989" size="18"></uni-icons>
			</view>
			<view v-if="!supplierBank.accountName" class="input-wrapper">
				<input disabled class="input" placeholder="请填写用于收款的银行账户信息" />
			</view>
			<view v-else class="form-wrapper">
				<view class="form-item">
					<view class="form-label">
						开户名
					</view>
					<view class="form-value">
						{{ supplierBank.accountName || '-' }}
					</view>
				</view>
				<view class="form-item">
					<view class="form-label">
						开户行
					</view>
					<view class="form-value">
						{{ supplierBank.depositBankName || '-' }}
					</view>
				</view>
				<view class="form-item">
					<view class="form-label">
						银行账号
					</view>
					<view class="form-value">
						{{ supplierBank.account || '-' }}
					</view>
				</view>
			</view>
		</view>


		<view class="btn-wrapper">
			<view class="btn default" @click="submit(0)">
				保存以防丢失
			</view>
			<view class="btn" @click="submit(1)">
				提交申请
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					id: '',
					supplierName: '',
					businessAddress: '',
					businessCategory: '',
					businessCategoryName: '',
					businessQualification: '',
					manager: '',
					managerTel: '',
					subjectType: '',
					serviceTel: '',
					supplyWarehouseId: '',
				},
				supplierAdmin: {
					realName: '',
					phone: '',
					supplierId: ''
				},
				supplierSubject: {},
				supplierLegal: {},
				supplierBank: {},
				disabled: false,
				isEdit: false
			}
		},
		computed: {
			userInfo() {
				return this.$store.state.userInfo;
			},
		},
		onLoad(option) {
			if (option.id) {
				this.isEdit = true;
				this.getInfo(option.id)
			} else {
				uni.setStorageSync('businessCategory', [])
			}
		},
		methods: {
			async getInfo(id) {
				const info = await this.$api.get('/adm/supplier/getInfo', {
					id: id
				})
				if (info.code === 200) {
					const data = info.data;
					this.form = {
						id: data.id,
						supplierId: data.supplierId,
						supplierCode: data.supplierCode,
						supplierName: data.supplierName,
						businessAddress: data.businessAddress || '',
						businessCategory: data.businessCategory || '',
						businessQualification: data.businessQualification || '',
						subjectType: data.subjectType,
						manager: data.manager || '',
						managerTel: data.managerTel || '',
						businessCategoryName: '',
						serviceTel: data.serviceTel || '',
					};
					if (this.form.businessCategory) {
						const businessCategory = JSON.parse(this.form.businessCategory)
						const array = businessCategory.map(item => item.categoryName)
						this.form.businessCategoryName = array.join()
						uni.setStorageSync('businessCategory', businessCategory)
					} else {
						uni.setStorageSync('businessCategory', [])
					}
					if (data.supplierAdmin) {
						this.supplierAdmin = {
							...data.supplierAdmin
						}
					}
					if (data.supplierSubject) {
						this.supplierSubject = {
							...data.supplierSubject
						}
					}
					if (data.supplierLegal) {
						this.supplierLegal = {
							...data.supplierLegal
						}
					}
					if (data.supplierBank) {
						this.supplierBank = {
							...data.supplierBank
						}
					}
				}
			},
			selectSubjectType() {
				uni.showActionSheet({
					itemList: ['自然人', '企业', '个体户'],
					success: (e) => {
						this.form.subjectType = e.tapIndex + 1;
					}
				})
			},
			goCategory() {
				uni.navigateTo({
					url: "/subPage_supplier/register/category"
				})
			},
			setCategory(array) {
				const arr = array.map(item => item.categoryName)
				this.form.businessCategory = JSON.stringify(array)
				this.form.businessCategoryName = arr.join();
				uni.setStorageSync('businessCategory', array)
			},
			goAdmin() {
				this.supplierAdmin.supplierId = this.form.id;
				uni.navigateTo({
					url: '/subPage_supplier/register/superAdmin?info=' + JSON.stringify(this.supplierAdmin)
				})
			},
			goOrg() {
				this.supplierSubject.subjectType = this.form.subjectType;
				this.supplierSubject.supplierId = this.form.id;
				uni.navigateTo({
					url: '/subPage_supplier/register/orgInfo?info=' + JSON.stringify(this.supplierSubject)
				})
			},
			setOrgData(info) {
				this.supplierSubject = {
					...info
				}
			},
			goLegal() {
				this.supplierLegal.supplierId = this.form.id;
				uni.navigateTo({
					url: '/subPage_supplier/register/legalPerson?info=' + JSON.stringify(this.supplierLegal)
				})
			},
			setLegalData(info) {
				this.supplierLegal = {
					...info
				}
			},
			goBank() {
				if (!this.form.subjectType) {
					uni.showToast({
						title: '请先选择主体类型',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (this.form.subjectType === 1 && !this.supplierAdmin.realName) {
					uni.showToast({
						title: '请先完善负责人信息',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (this.form.subjectType !== 1 && !this.supplierSubject.subjectName) {
					uni.showToast({
						title: '请先完善经营主体',
						icon: 'none'
					})
					return
				}
				if (this.form.subjectType !== 1 && !this.supplierLegal.realName) {
					uni.showToast({
						title: '请先完善法人信息',
						icon: 'none'
					})
					return
				}
				this.supplierBank.supplierId = this.form.id;
				let supplierInfo = {
					supplierAdmin: {
						...this.supplierAdmin
					},
					supplierSubject: {
						...this.supplierSubject
					},
					supplierLegal: {
						...this.supplierLegal
					}
				}
				uni.setStorageSync('supplierInfo_register', supplierInfo)
				uni.navigateTo({
					url: '/subPage_supplier/register/bankAccount?type=' + this.form.subjectType + '&info=' + JSON
						.stringify(this.supplierBank)
				})
			},
			setBankData(info) {
				this.supplierBank = {
					...info
				}
			},
			setSuperAdminInfo(info) {
				this.supplierAdmin = {
					...info
				}
			},
			goBusinessQualification() {
				uni.navigateTo({
					url: '/subPage_supplier/register/businessQualification?list=' + this.form.businessQualification
				})
			},
			setBusinessQualification(context) {
				this.form.businessQualification = context;
			},
			async submit(status) {
				if (this.disabled) return
				const params = {
					...this.form
				}
				if (!params.supplierName) {
					uni.showToast({
						title: '请输入供应商名称',
						icon: 'none'
					})
					return
				}
				if (status === 1) {
					if (!params.businessAddress) {
						uni.showToast({
							title: '请输入经营地址',
							icon: 'none'
						})
						return
					}
					if (!params.manager || !params.managerTel) {
						uni.showToast({
							title: '请完善管理员信息',
							icon: 'none'
						})
						return
					}
					if (!params.businessCategory) {
						uni.showToast({
							title: '请选择经营类目',
							icon: 'none'
						})
						return
					}
					if (!params.supplierName) {
						uni.showToast({
							title: '请选择主体类型',
							icon: 'none'
						})
						return
					}

					if (this.form.subjectType === 1) {
						if (!this.supplierAdmin.realName) {
							uni.showToast({
								title: '请完善负责人信息',
								icon: 'none'
							})
							return
						}
					} else {
						if (!this.supplierSubject.subjectName) {
							uni.showToast({
								title: '请完善经营主体信息',
								icon: 'none'
							})
							return
						}
						if (!this.supplierLegal.realName) {
							uni.showToast({
								title: '请完善法人信息',
								icon: 'none'
							})
							return
						}
					}

					if (!this.supplierBank.accountName) {
						uni.showToast({
							title: '请完善收款账户',
							icon: 'none'
						})
						return
					}
					params.examineOpinion = 'null';
				} else {
					if (!params.manager || !params.managerTel) {
						uni.showToast({
							title: '请完善管理员信息',
							icon: 'none'
						})
						return
					}
				}
				if (this.supplierAdmin.realName) {
					params.supplierAdmin = this.supplierAdmin;
				}
				if (this.supplierSubject.subjectName) {
					params.supplierSubject = this.supplierSubject;
				}
				if (this.supplierLegal.realName) {
					params.supplierLegal = this.supplierLegal;
				}
				if (this.supplierBank.accountName) {
					params.supplierBank = this.supplierBank;
				}
				this.disabled = true;
				params.supplierStatus = status;
				let data;
				if (params.id) {
					data = await this.$api.put('/adm/supplier', params)
				} else {
					params.operationSpecialistName = this.userInfo.nickName;
					params.operationSpecialistTel = this.userInfo.phonenumber;
					params.operationSpecialistId = this.userInfo.userId;
					params.supplyWarehouseId = this.userInfo.supplyWarehouseId;
					data = await this.$api.post('/adm/supplier', params)
				}
				if (data.code === 200) {
					if (this.isEdit) {
						uni.showToast({
							title: '操作成功'
						})
						var pages = getCurrentPages();
						var prevPage = pages[pages.length - 2];
						if (prevPage == undefined || prevPage == null) return;
						setTimeout(() => {
							prevPage.$vm.refresh();
							uni.navigateBack();
						}, 1000)
						return
					}
					if (status === 0) {
						uni.showToast({
							title: '保存成功'
						})
						this.disabled = false;
						if (!params.id) {
							this.form.id = data.data;
							this.supplierAdmin.supplierId = data.data;
						}
					} else {
						uni.showToast({
							title: '申请成功'
						})
						uni.navigateBack()
					}
				} else {
					this.disabled = false;
				}
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.step1 {
		padding-bottom: 130upx;

		.item-wrapper {
			background-color: #ffffff;
			margin: 20upx 20upx 0 20upx;
			border-radius: 20upx;
			padding: 25upx;

			&.error {
				box-shadow: 0 0 7upx red;
			}

			.title-wrapper {
				display: flex;
				align-items: center;

				.icon {
					margin-right: 5upx;
				}

				.title {
					height: 34upx;
					line-height: 34upx;
					font-size: 34upx;
					color: #222222;
					padding-left: 10upx;
					border-left: 8upx solid #555BFF;
				}

				.value {
					flex: 1;
					text-align: right;
					font-size: 32upx;
					color: #666666;

					&.black {
						color: #222222;
					}
				}

				.input {
					flex: 1;
					text-align: right;
					font-size: 32upx;
					color: #666666;
					margin-right: 10upx;
				}
			}

			.input-wrapper {
				padding-top: 15upx;

				.input {
					font-size: 32upx;
					padding: 10upx;
					height: 60upx;
				}

				.text-area {
					width: 100%;
					font-size: 32upx;
					height: 60upx;
					padding: 10upx;
				}
			}

			.tip {
				font-size: 26upx;
				color: #898989;
			}

			.form-wrapper {
				padding: 0 20upx;

				.form-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					font-size: 32upx;
					margin-top: 30upx;

					.form-label {
						width: 170upx;
						color: #232323;
					}

					.form-value {
						flex: 1;
						color: #666666;
					}
				}
			}
		}

		.btn-wrapper {
			position: fixed;
			height: 100upx;
			width: 100%;
			bottom: 0;
			background-color: #ffffff;
			display: flex;
			align-items: center;
			z-index: 1;

			.btn {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				height: 100%;
				background-color: #555BFF;
				color: #ffffff;

				&.default {
					color: #232323;
					background-color: #ffffff;
				}
			}
		}
	}
</style>